<%@ page import="com.pc.cloud.services.FacetQueryArg" %>
<%@ page import="com.pc.cloud.services.FacetSearchRow" %>
<%@ page import="com.pc.cloud.services.SearchResults" %>
<%@ page import="com.pc.cloud.services.impl.LocalSearchServices" %>
<%@ page import="com.pc.cloud.utils.NamePair" %>
<%@ page import="com.pc.tas.learning.trace.index.TraceSearchArgs" %>
<%@ page import="com.pc.tas.util.SpringBeanUtil" %>
<%@ page import="org.json.JSONObject" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Collection" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.pc.cloud.services.MetricArg" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>

<%
    LocalSearchServices searchServices = (LocalSearchServices) SpringBeanUtil.getBean("searchServices");
    TraceSearchArgs tsa = new TraceSearchArgs();
    // tsa.setId("2013042650100");
    List<MetricArg> metricFields = new ArrayList<MetricArg>();
    metricFields.add(new MetricArg("id", MetricArg.COUNT));
    metricFields.add(new MetricArg("ip", MetricArg.COUNT));
    tsa.setMetricFields(metricFields);

    FacetQueryArg fqa = new FacetQueryArg();
    fqa.setFacetField("page_id");
    fqa.setMetricFields(metricFields);
    fqa.setOffset(0);//分页 ，第几页
    fqa.setLimit(20);//分页  。每页多少条记录
    /*if(sort != null){
                         fqa.setSort(sort);//排序指标
                     }*/
    tsa.getFacetQueryFields().add(fqa);
    SearchResults res = searchServices.search(tsa);
    Map<String, Collection<FacetSearchRow>> sumFacets = res.getSumFacets();
    if (sumFacets != null && sumFacets.size() > 0) {
        for (Map.Entry<String, Collection<FacetSearchRow>> entry : sumFacets.entrySet()) {
            Collection<FacetSearchRow> fsrs = entry.getValue();
            for (FacetSearchRow fsr : fsrs) {
                int total = fsr.getTotal();
                JSONObject jo = new JSONObject();
                String names = fsr.getName();
                out.println("name:" + names);
                Collection<NamePair> nps = fsr.getPairs();
                for (NamePair np : nps) {
                    String name = np.getName();
                    jo.put(name, np.getValue());
                }
                out.println("jo:" + jo.toString());
            }
        }
    }
%>
